﻿using System;
using System.Collections.Generic;
using System.Text;
using SlamDunk.CMS.Common;

namespace SlamDunk.CMS.BLL
{
    /// <summary>
    /// Channel业务逻辑层类（手写部分类）
    /// </summary>
    public partial class ChannelBll
    {
        #region 01.根据条件获取记录条数
        /// <summary>
        /// 根据条件获取记录条数
        /// </summary>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>记录条数</returns>
		public int GetCount(string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!arg1.IsNullOrEmpty() && arg2.IsNullOrEmpty() && arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                return CurrentDal.GetCount("Field1", arg1);
            }
            
            //2个参数
            else if (!arg1.IsNullOrEmpty() && !arg2.IsNullOrEmpty() && arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                return CurrentDal.GetCount("Field1", arg1, "Field2", arg2);
            }
            
            //3个参数
            else if (!arg1.IsNullOrEmpty() && !arg2.IsNullOrEmpty() && !arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                return CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3);
            }
            
            //4个参数
            else
            {
                return CurrentDal.GetCount("Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
            }
        }
        #endregion
        
        #region 02.根据条件分页获取实体对象集合
        /// <summary>
        /// 根据条件分页获取实体对象集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="rowCount">总记录条数</param>
        /// <param name="arg1">条件参数</param>
        /// <param name="arg2">条件参数</param>
        /// <param name="arg3">条件参数</param>
        /// <param name="arg4">条件参数</param>
        /// <returns>实体对象集合</returns>
		public List<Model.Channel> GetModelList(int pageIndex, int pageSize, out int pageCount, out int rowCount, string arg1, string arg2, string arg3, string arg4)
        {
            //1个参数
            if (!arg1.IsNullOrEmpty() && arg2.IsNullOrEmpty() && arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, null, null, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return CurrentDal.GetModelList(start, end, "Field1", arg1);
            }

            //2个参数
            else if (!arg1.IsNullOrEmpty() && !arg2.IsNullOrEmpty() && arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, null, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return CurrentDal.GetModelList(start, end, "Field1", arg1, "Field2", arg2);
            }

            //3个参数
            else if (!arg1.IsNullOrEmpty() && !arg2.IsNullOrEmpty() && !arg3.IsNullOrEmpty() && arg4.IsNullOrEmpty())
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, arg3, null);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return CurrentDal.GetModelList(start, end, "Field1", arg1, "Field2", arg2, "Field3", arg3);
            }

            //4个参数
            else
            {
                //1.计算总记录条数与总页数
                rowCount = this.GetCount(arg1, arg2, arg3, arg4);
                pageCount = (int)Math.Ceiling((rowCount * 1.0 / pageSize));
                //2.页索引处理
                pageIndex = pageIndex < 1 ? 1 : pageIndex;
                pageIndex = pageIndex >= pageCount ? pageCount : pageIndex;
                //3.计算起始行与终止行
                int start = (pageIndex - 1) * pageSize + 1;
                int end = pageIndex * pageSize;
                //4.调用数据层返回相应实体集合
                return CurrentDal.GetModelList(start, end, "Field1", arg1, "Field2", arg2, "Field3", arg3, "Field4", arg4);
            }
        }
        #endregion

        #region 03.根据父Id查询所有栏目列表
        /// <summary>
        /// 根据父Id获取栏目列表
        /// </summary>
        /// <param name="pId">父Id</param>
        /// <returns>栏目列表</returns>
        public List<Model.Channel> GetModelList(int pId)
        {
            return CurrentDal.GetModelList(pId);
        }
        #endregion

        #region 04.根据栏目对象获取文章总页数
        /// <summary>
        /// 根据栏目对象获取文章总页数
        /// </summary>
        /// <param name="model">栏目对象</param>
        /// <returns>符合条件的文章页数</returns>
        public int GetPageCount(Model.Channel model)
        {
            ArticleBll ArticleBll = ArticleBll.CreateInstance();
            int rowCount = ArticleBll.GetCount(model.Id);
            int pageCount = (int)Math.Ceiling(rowCount * 1.0/ model.PageSize);
            return pageCount;
        }
        #endregion
    }
}
